import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.Paint;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
//public class MyEdge
//{
//	int from,to;
//	MyEdge(int from,int to)
//	{
//		this.from=from;
//		this.to=to;
//	}
//}
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

public class MyGraph 
{
	

 int pictureWidth,pictureHeight;
 List<Integer>LastDjikstraVertexList;
 //List<MyEdge>LastDjikstraEdgeList;
 int []Vertcolor;
 int []Edgecolor;
 int [][]Weights;
 Graph g;
 int Vsize;
 JPanel drawframe;
 GridBagConstraints layout;
 Container drawpanel;
 public MyGraph(DefaultTableModel t,GridBagConstraints layout,Container hz,int[]Vertcolor,int []Edgecolor,boolean Directed)
 {
	drawpanel=hz;
	this.layout=layout;
	Weights=new int[t.getRowCount()][t.getColumnCount()];
	for(int i=0;i<t.getRowCount();i++)
	{
		for(int j=0;j<t.getColumnCount();j++)
		{
			Weights[i][j]=Integer.valueOf(t.getValueAt(i, j).toString());
		}
	}
	
	//fill graph:!
	
    MyNode[]nodes=new MyNode[t.getRowCount()];
    System.out.println(t.getRowCount());
    Vsize=nodes.length;
    if(Directed)
    {
    g = new DirectedSparseMultigraph<Integer, MyLink>();
    }
    else g=new SparseMultigraph<Integer, MyLink>();
   
     for(int i=1;i<=Vsize;i++)
	 {
	   g.addVertex((Integer)i);
	 }
     for(int i=0;i<Vsize;i++)
     {
    	 for(int j=i+1;j<Vsize;j++)
    	 {
    		 if(Weights[i][j]!=-1)
    		 {
    			 g.addEdge(new MyLink(Weights[i][j]),i+1,j+1);
    		 }
    	 }
     }
     
 }
 
 public void Draw()
 {
	 System.out.println(drawpanel.getComponentCount());
	 if(drawpanel.getComponentCount()>3)
	 {
		 
		 drawpanel.remove(3);
	 }
	 System.out.println(drawpanel.getComponentCount());
	 
	 //int width=drawframe.getWidth();
	 //int height=drawframe.getHeight();
	 //System.out.println("The graph g = " + g.toString());
	 Layout<Integer, MyLink> layout1 = new ISOMLayout<Integer, MyLink>(g);	
	 layout1.setSize(new Dimension(drawpanel.getWidth()/2,drawpanel.getHeight()-50));
	 BasicVisualizationServer<Integer,MyLink> vv =
	 new BasicVisualizationServer<Integer,MyLink>(layout1);
	 vv.setSize(new Dimension(200,200));
	// layout1.setSize(new Dimension(200,200));
	 Transformer<Integer,Paint> vertexPaint = new Transformer<Integer,Paint>() {
	 public Paint transform(Integer x) {
	      if(x==1)return Color.GREEN;
	      else return Color.RED;
		}
		};
		
		// Set up a new stroke Transformer for the edges
		float dash[] = {10000.0f};
		float dash2[] = {1000.0f};
		final Stroke edgeStroke1 = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
		BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
		final Stroke edgeStroke2 = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
		Transformer<MyLink, Stroke> edgeStrokeTransformer =
		new Transformer<MyLink, Stroke>() {
		public Stroke transform(MyLink s) {
				return edgeStroke1;
		}
		};
		vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
		vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);
		vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
		vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
		vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
		//vv.setFont(new Font())
		this.layout.gridx=1;
		this.layout.gridy=1;
		this.layout.weightx=0.0;
		this.layout.gridy=1;
	    this.layout.gridx=1;
	    this.layout.anchor=GridBagConstraints.FIRST_LINE_START;
	  
	    try {
			Thread.currentThread().sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(vv.getSize());
		vv.setBackground(Color.WHITE);
	    drawpanel.add(vv,this.layout);
	  
		 
	 
 }
 //`public JPanel 
 
}
